
<div id="contains">
	<div class="title"></div>
	<div class="content_code">
		<div class="approval-right" style="padding-right: 10px">

			<form action="salary.php?content=import_checktime" method="post"
				name="formexcel" enctype="multipart/form-data">
				<div style="float: left">
					<h3>Nhap tu file excel</h3>
				</div>
				<input name="fileexcel" type="file" value="ad"
					style="margin-left: 10px" onchange="document.formexcel.submit()" />
			</form>

			<?php
			if (isset($_FILES["fileexcel"])) {
				unset($_SESSION['mysheets']);
				include 'CheckTime.php';
				include 'Classes/PHPExcel.php';

				$inputFileName = $_FILES['fileexcel']['tmp_name'];
				//$inputFileName = 'C://AppServ/www/ERP/ERP-PC/mau_saved.xlsx';
				$objPHPExcel = PHPExcel_IOFactory::load($inputFileName);//content_salary/CheckTimeSave.php
				echo '<div style="padding:3px 0"> <a href="content_salary/CheckTimeSave.php"><input type="button" value="Save" class="btnupload btnupbg"/></a></div>';
				echo '<table style="width:100%;" class="rf-dt"><tr>';
				echo '<td class="rf-dt-shdr-c" width="20%">Name</td>';
				echo '<td class="rf-dt-shdr-c" width="9%">ID</td>';
				echo '<td class="rf-dt-shdr-c" width="10%">Date</td>';
				echo '<td class="rf-dt-shdr-c" width="8%">In</td>';
				echo '<td class="rf-dt-shdr-c" width="8%">Out</td>';
				echo '<td class="rf-dt-shdr-c" width="5%">Late</td>';
				echo '<td class="rf-dt-shdr-c" width="5%">Early</td>';
				echo '<td class="rf-dt-shdr-c" width="5%">Off half day</td>';
				echo '<td class="rf-dt-shdr-c" width="5%">Off a day</td>';
				echo '<td class="rf-dt-shdr-c" width="5%">Overtime</td>';
				echo '<td class="rf-dt-shdr-c" width="20%">Note</td></tr>';
					
				$loadedSheetNames = $objPHPExcel->getSheetNames();
				$objects = array();
				foreach($loadedSheetNames as $sheetIndex => $loadedSheetName) {

					$highestRow = $objPHPExcel->setActiveSheetIndexByName($loadedSheetName)->getHighestRow();
					if ($highestRow > 2) {
							
						$objPHPExcel->getActiveSheet()->getStyle(sprintf('C3:C%s',$highestRow))->getNumberFormat()->setFormatCode('mm/dd/YYYY');
						//check time exist
						$date1 = PHPExcel_Shared_Date::ExcelToPHPObject($objPHPExcel->getActiveSheet()->getCell("C3")->getValue());
						$dateFormat = $date1->format('m/d/Y');
						$mdate = explode("/", $dateFormat);
						$sheetdate = $mdate[2]."-".$mdate[0]."-".$mdate[1];
						echo '<tr><td colspan="11" class="rf-dt-c"><b>Worksheet -> ',$loadedSheetName,'</b>';
						echo $sheetdate;
						//$checkTimeLog = mysql_query("select id from sa_check_time_log WHERE id=".$usedate);
						echo '</td></tr>';
						$sheetData = $objPHPExcel->getActiveSheet()->toArray(null,true,true,true);
						$index =0;
						$mySheets = array();
						foreach ($sheetData as $key => $row){
							if ($index > 1) {
								$object = new CheckTime();
								$object->setDate($dateFormat,$row['C']);
								$object->employeeID = $row['A'];
								$object->employeeName = $row['B'];
								$object->inTime($row['D']);
								$object->outTime($row['E']);
								$object->off($row['D'], $row['E']);

								echo '<tr>';
								echo '<td class="rf-dt-c">';
								echo $object->employeeName;
								echo '</td>';

								echo '<td class="rf-dt-c">';
								echo $object->employeeID;
								echo '</td>';

								echo '<td class="rf-dt-c">';

								echo $object->date;
								echo '</td>';

								echo '<td class="rf-dt-c">';
								if ($object->inTime != '0') echo $object->inTime;
								echo '</td>';

								echo '<td class="rf-dt-c">';
								if ($object->outTime != '0') echo $object->outTime;
								echo '</td>';

								echo '<td class="rf-dt-c">';
								if ($object->late != '0') echo $object->late;
								echo '</td>';

								echo '<td class="rf-dt-c">';
								if ($object->early != '0') echo $object->early;
								echo '</td>';

								echo '<td class="rf-dt-c">';
								if ($object->halfDayOff != '0') echo $object->halfDayOff;
								echo '</td>';

								echo '<td class="rf-dt-c">';
								if ($object->aDayOff != '0') echo $object->aDayOff;
								echo '</td>';

								echo '<td class="rf-dt-c">';
								if ($object->overtime != '0') echo $object->overtime;
								echo '</td>';

								echo '<td class="rf-dt-c">';
								echo $object->note;
								echo '</td>';
								echo '</tr>';
								array_push($mySheets, $object);
							}
							$index += 1;
						}

						$objects[$loadedSheetName] = $mySheets;
					}


				}
				$_SESSION['mysheets'] = $objects;
				echo '</table>';
			}elseif (isset($_SESSION['mysheets']) && is_array($_SESSION['mysheets'])){
				include 'CheckTime.php';
				include '../operations/connection.php';
				$tempsql = "";
				echo '<div style="padding:3px 0"> <a href="content_salary/CheckTimeSave.php"><input type="button" value="Save" class="btnupload btnupbg"/></a></div>';
				echo '<table style="width:100%;" class="rf-dt"><tr>';
				echo '<td class="rf-dt-shdr-c" width="20%">Name</td>';
				echo '<td class="rf-dt-shdr-c" width="9%">ID</td>';
				echo '<td class="rf-dt-shdr-c" width="10%">Date</td>';
				echo '<td class="rf-dt-shdr-c" width="8%">In</td>';
				echo '<td class="rf-dt-shdr-c" width="8%">Out</td>';
				echo '<td class="rf-dt-shdr-c" width="5%">Late</td>';
				echo '<td class="rf-dt-shdr-c" width="5%">Early</td>';
				echo '<td class="rf-dt-shdr-c" width="5%">Off half day</td>';
				echo '<td class="rf-dt-shdr-c" width="5%">Off a day</td>';
				echo '<td class="rf-dt-shdr-c" width="5%">Overtime</td>';
				echo '<td class="rf-dt-shdr-c" width="20%">Note</td></tr>';
				foreach ($_SESSION['mysheets'] as $sheetkey =>  $sheetobject){
					echo '<tr><td colspan="11" class="rf-dt-c"><b>Worksheet -> ',$sheetkey,'</b></tr>';
					foreach ($sheetobject as $key =>  $object){
						$CT = unserialize (serialize ($object));
						echo '<tr>';
						echo '<td class="rf-dt-c">';
						echo $CT->employeeName;
						echo '</td>';

						echo '<td class="rf-dt-c">';
						echo $CT->employeeID;
						echo '</td>';

						echo '<td class="rf-dt-c">';

						echo $CT->date;
						echo '</td>';

						echo '<td class="rf-dt-c">';
						if ($CT->inTime != '0') echo $CT->inTime;
						echo '</td>';

						echo '<td class="rf-dt-c">';
						if ($CT->outTime != '0') echo $CT->outTime;
						echo '</td>';

						echo '<td class="rf-dt-c">';
						if ($CT->late != '0') echo $CT->late;
						echo '</td>';

						echo '<td class="rf-dt-c">';
						if ($CT->early != '0') echo $CT->early;
						echo '</td>';

						echo '<td class="rf-dt-c">';
						if ($CT->halfDayOff != '0') echo $CT->halfDayOff;
						echo '</td>';

						echo '<td class="rf-dt-c">';
						if ($CT->aDayOff != '0') echo $CT->aDayOff;
						echo '</td>';

						echo '<td class="rf-dt-c">';
						if ($CT->overtime != '0') echo $CT->overtime;
						echo '</td>';

						echo '<td class="rf-dt-c">';
						echo $CT->note;
						echo '</td>';
						echo '</tr>';
					}
				}
				echo '</table>';
			}

			?>

		</div>
	</div>
</div>
